package hanlder

import (
	"design-x/model"
	"github.com/gin-gonic/gin"
	"strconv"
)

func Add(c *gin.Context) {
	name := c.PostForm("name")
	shopNum, _ := strconv.Atoi(c.PostForm("shopNum"))
	types := c.PostForm("type")

	if name == "" || shopNum == 0 || types == "" {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "不能为空",
		})
		return
	}
	data := model.Shop{
		Name:    name,
		ShopNum: shopNum,
		Types:   types,
	}
	err := model.DB.Create(&data).Debug().Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "发布失败",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "发布成功",
	})
	return
}
func List(c *gin.Context) {
	page, _ := strconv.Atoi(c.Query("page"))
	size, _ := strconv.Atoi(c.Query("size"))
	offset := (page - 1) * size

	var shop []model.Shop
	model.DB.Offset(offset).Limit(size).Find(&shop)

	c.JSON(200, gin.H{
		"code": 200,
		"data": shop,
		"msg":  "展示成功",
	})
	return
}
